'''
Created on Jul 20, 2012

@author: LENOVO
''' 
#TODO: make sure that we don't exceed 100 queries per day
#TODO: make sure that we can log the errors
from apiclient.discovery import build

# Build a service object for interacting with the API. Visit
# the Google APIs Console <http://code.google.com/apis/console>
# to get an API key for your own application.
class Login(object):
    '''
    This class receives a query and asks a search engine for the response.
    In this case the search engine is google
    '''
    
    ENGINE_ID = '010011625994867416317:mggvzbfiab4'
    APP_KEY = 'AIzaSyC_pbBWgL_mQ_686i0sZLRUdarsq8MgNPg'
    SITE = 'http://en.wikipedia.org'
    
    def __init__(self):
        self.service = build("customsearch", "v1",developerKey=Login.APP_KEY)
    
    def question(self,toAsk): 
        res = self.service.cse().list(q=toAsk,cx=Login.ENGINE_ID,siteSearch=Login.SITE,num=1).execute()
        siteAnswer = res['items'][0]['htmlTitle']
        resIndex = siteAnswer.find('-')
        print siteAnswer[:resIndex]